#!/bin/sh

# FTP credentials and GnuPG passphrase
FTP_HOST="ftp.example.com"
FTP_USER="backup"
export FTP_PASSWORD="(insert your password here)" # Note: Environment variable used by duplicity
export PASSPHRASE="(insert your passphrase here)" # Note: Environment variable used by duplicity

# Directories to backup
MEDIA_DIR="/srv/http/org/nordicmicroalgae/media/files"
DOWNLOADS_DIR="/srv/http/org/nordicmicroalgae/downloads/public"
DRUPAL_FILES_DIR="/srv/http/org/nordicmicroalgae/public/sites/default/files"
DB_DUMPS_DIR="$HOME/dbdumps"

# dump database
mysqldump -u nordicmicroalgae --single-transaction --opt --default-character-set=utf8 nordicmicroalgae_cms > "$DB_DUMPS_DIR"/nordicmicroalgae_cms.bak.sql

# doing full backup monthly
duplicity --full-if-older-than 1M "$MEDIA_DIR" "ftp://$FTP_USER@$FTP_HOST/media"
duplicity --full-if-older-than 1M "$DOWNLOADS_DIR" "ftp://$FTP_USER@$FTP_HOST/downloads"
duplicity --full-if-older-than 1M "$DRUPAL_FILES_DIR" "ftp://$FTP_USER@$FTP_HOST/drupalfiles"
duplicity --full-if-older-than 1M "$DB_DUMPS_DIR" "ftp://$FTP_USER@$FTP_HOST/dbdumps"

# clean up on the remote once a year
duplicity remove-older-than 1Y --force "ftp://$FTP_USER@$FTP_HOST/media"
duplicity remove-older-than 1Y --force "ftp://$FTP_USER@$FTP_HOST/downloads"
duplicity remove-older-than 1Y --force "ftp://$FTP_USER@$FTP_HOST/drupalfiles"
duplicity remove-older-than 1Y --force "ftp://$FTP_USER@$FTP_HOST/dbdumps"

unset FTP_PASSWORD
unset PASSPHRASE
